<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/src/corelib/thread/qmutex.cpp -->
<head>
  <title>Qt 4.3: QMutexLocker Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">QMutexLocker Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1>
<p>The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes. <a href="#details">More...</a></p>
<pre> #include &lt;QMutexLocker&gt;</pre><p><b>Note:</b> All the functions in this class are <a href="threads.html#thread-safe">thread-safe</a>.</p>
<ul>
<li><a href="qmutexlocker-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h3>Public Functions</h3>
<ul>
<li><div class="fn"/><b><a href="qmutexlocker.html#QMutexLocker">QMutexLocker</a></b> ( QMutex * <i>mutex</i> )</li>
<li><div class="fn"/><b><a href="qmutexlocker.html#dtor.QMutexLocker">~QMutexLocker</a></b> ()</li>
<li><div class="fn"/>QMutex * <b><a href="qmutexlocker.html#mutex">mutex</a></b> () const</li>
<li><div class="fn"/>void <b><a href="qmutexlocker.html#relock">relock</a></b> ()</li>
<li><div class="fn"/>void <b><a href="qmutexlocker.html#unlock">unlock</a></b> ()</li>
</ul>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.</p>
<p>Locking and unlocking a <a href="qmutex.html">QMutex</a> in complex functions and statements or in exception handling code is error-prone and difficult to debug. QMutexLocker can be used in such situations to ensure that the state of the mutex is always well-defined.</p>
<p>QMutexLocker should be created within a function where a <a href="qmutex.html">QMutex</a> needs to be locked. The mutex is locked when QMutexLocker is created. You can unlock and relock the mutex with <tt>unlock()</tt> and <tt>relock()</tt>. If locked, the mutex will be unlocked when the QMutexLocker is destroyed.</p>
<p>For example, this complex function locks a <a href="qmutex.html">QMutex</a> upon entering the function and unlocks the mutex at all the exit points:</p>
<pre> int complexFunction(int flag)
 {
     mutex.lock();

     int retVal = 0;

     switch (flag) {
     case 0:
     case 1:
         mutex.unlock();
         return moreComplexFunction(flag);
     case 2:
         {
             int status = anotherFunction();
             if (status &lt; 0) {
                 mutex.unlock();
                 return -2;
             }
             retVal = status + flag;
         }
         break;
     default:
         if (flag &gt; 10) {
             mutex.unlock();
             return -1;
         }
         break;
     }

     mutex.unlock();
     return retVal;
 }</pre>
<p>This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.</p>
<p>Using QMutexLocker greatly simplifies the code, and makes it more readable:</p>
<pre> int complexFunction(int flag)
 {
     QMutexLocker locker(&amp;mutex);

     int retVal = 0;

     switch (flag) {
     case 0:
     case 1:
         return moreComplexFunction(flag);
     case 2:
         {
             int status = anotherFunction();
             if (status &lt; 0)
                 return -2;
             retVal = status + flag;
         }
         break;
     default:
         if (flag &gt; 10)
             return -1;
         break;
     }

     return retVal;
 }</pre>
<p>Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since <tt>locker</tt> is an auto variable).</p>
<p>The same principle applies to code that throws and catches exceptions. An exception that is not caught in the function that has locked the mutex has no way of unlocking the mutex before the exception is passed up the stack to the calling function.</p>
<p>QMutexLocker also provides a <tt>mutex()</tt> member function that returns the mutex on which the QMutexLocker is operating. This is useful for code that needs access to the mutex, such as <a href="qwaitcondition.html#wait">QWaitCondition::wait</a>(). For example:</p>
<pre> class SignalWaiter
 {
 private:
     QMutexLocker locker;

 public:
     SignalWaiter(QMutex *mutex)
         : locker(mutex)
     {
     }

     void waitForSignal()
     {
         ...
         while (!signalled)
             waitCondition.wait(locker.mutex());
         ...
     }
 };</pre>
<p>See also <a href="qreadlocker.html">QReadLocker</a>, <a href="qwritelocker.html">QWriteLocker</a>, and <a href="qmutex.html">QMutex</a>.</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="QMutexLocker"></a>QMutexLocker::QMutexLocker ( <a href="qmutex.html">QMutex</a> * <i>mutex</i> )</h3>
<p>Constructs a <a href="qmutexlocker.html">QMutexLocker</a> and locks <i>mutex</i>. The mutex will be unlocked when the <a href="qmutexlocker.html">QMutexLocker</a> is destroyed. If <i>mutex</i> is zero, <a href="qmutexlocker.html">QMutexLocker</a> does nothing.</p>
<p>See also <a href="qmutex.html#lock">QMutex::lock</a>().</p>
<h3 class="fn"><a name="dtor.QMutexLocker"></a>QMutexLocker::~QMutexLocker ()</h3>
<p>Destroys the <a href="qmutexlocker.html">QMutexLocker</a> and unlocks the mutex that was locked in the constructor.</p>
<p>See also <a href="qmutex.html#unlock">QMutex::unlock</a>().</p>
<h3 class="fn"><a name="mutex"></a><a href="qmutex.html">QMutex</a> * QMutexLocker::mutex () const</h3>
<p>Returns a pointer to the mutex that was locked in the constructor.</p>
<h3 class="fn"><a name="relock"></a>void QMutexLocker::relock ()</h3>
<p>Relocks an unlocked mutex locker.</p>
<p>See also <a href="qmutexlocker.html#unlock">unlock</a>().</p>
<h3 class="fn"><a name="unlock"></a>void QMutexLocker::unlock ()</h3>
<p>Unlocks this mutex locker. You can use <tt>relock()</tt> to lock it again. It does not need to be locked when destroyed.</p>
<p>See also <a href="qmutexlocker.html#relock">relock</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
